using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin2022Soft2.Application.Common.Interface;
using Admin2022Soft2.Application.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Admin2022Soft2.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class FilesController : ControllerBase
    {
        private readonly IAppFileUploadService _appFileService;

        public FilesController(IAppFileUploadService appFileService)
        {
            _appFileService = appFileService;
        }


        [HttpPost]
        [Route("/files")]
        public async Task<string> OnPostUploadAsync(IFormCollection files)
        {
            var list = await _appFileService.UploadFile(files);
            if (list.Count() > 0)
            {
                return new
                {
                    Code = 1000,
                    Msg = "上传成功",
                    Data = list
                }.SerializeObject();
            }
            else
            {
                return new
                {
                    Code = 4000,
                    Msg = "上传失败，文件扩展名不被允许、文件大小不被允许",
                    Data = list
                }.SerializeObject();
            }
        }

        [AllowAnonymous]
        [HttpGet]
        [Route("/files/{id}")]
        public async Task<FileContentResult> GetFile(Guid id)
        {
            var result = await _appFileService.GetFileById(id);
            return result;
        }

    }
}